package com.dingreading.cloud.admin.controller;

import com.dingreading.cloud.admin.entity.CourseVideo;
import com.dingreading.cloud.admin.service.CourseVideoService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.AccountUtil;
import com.dingreading.cloud.common.util.PageBeanDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.aliyun.OSSClientUtil;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/system/courseVideo")
@Api(tags = "总部-学院课程的视频管理")
public class CourseVideoController {

    @Resource
    private CourseVideoService courseVideoService;

    @Value("${envi}")
    private String envi;

    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/pageList", produces = "application/json; charset=UTF-8")
    public R<Object> pageList(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "分页公共类") PageUtil pageUtil,
            @ApiParam(value = "视频名称") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "课程id") @RequestParam(value = "courseId", required = false) Long courseId,
            @ApiParam(value = "启用状态") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        Page<CourseVideo> page = this.courseVideoService.pageList(pageUtil, courseId, keyword, enabled);

        PageBeanDto data = new PageBeanDto();
        data.setTotal(page.getTotalRow());
        data.setList(page.getRecords());
        return R.ok(data);
    }


    @ApiOperation(value = "新增/修改课程视频")
    @PostMapping(value = "/newVideo")
    public R<Object> newVideo(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "课程id") @RequestParam(value = "courseId") Long courseId,
            @ApiParam(value = "视频名称") @RequestParam(value = "name") String name,
            @ApiParam(value = "视频地址") @RequestParam(value = "url") String url,
            @ApiParam(value = "序号") @RequestParam(value = "orderNum", required = false) Integer orderNum

    ) {
        if (id == null || id <= 0) {
            // 新增
            // 获取最大的序号
            Integer maxOrderNum = courseVideoService.getMaxShowOrder();

            CourseVideo data = new CourseVideo();
            data.setName(name);
            data.setUrl(url);
            data.setCourseId(courseId);
            data.setLookNum(0);
            data.setEnabled(1);
            data.setOrderNum(maxOrderNum);
            courseVideoService.saveOrUpdate(data);
        } else {
            // 修改
            CourseVideo data = courseVideoService.getById(id);
            if (data == null) return R.fail("获取失败");

            String exUrl = data.getUrl();

            if (name != null)
                data.setName(name);
            if (url != null)
                data.setUrl(url);
            data.setCourseId(courseId);
            data.setOrderNum(orderNum);
            courseVideoService.saveOrUpdate(data);

            // 上传的文件路径和原文件路径不一样
//            if (!exUrl.equals(url)) {
//                // 其他课次没有使用视频地址则删除阿里云文件
//                boolean b = courseVideoService.haveUrl(exUrl);
//                if (b)
//                    deleteOssFile(exUrl);
//            }
        }
        return R.ok();
    }


    @ApiOperation(value = "根据ID获取信息")
    @PostMapping(value = "/getById", produces = "application/json; charset=UTF-8")
    public R<Object> getById(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
        CourseVideo data = courseVideoService.getById(id);
        return R.ok(data);
    }


    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        courseVideoService.updateEnabled(id, enabled);
        return R.ok();
    }


    @ApiOperation(value = "根据课程ID获取视频")
    @PostMapping(value = "/getByCourseId", produces = "application/json; charset=UTF-8")
    public R<Object> getByCourseId(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "课程id") @RequestParam(value = "courseId") Long courseId
    ) {
        List<CourseVideo> data = courseVideoService.getByCourseId(courseId, null);
        return R.ok(data);
    }


    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "总部员工uid") @RequestParam(value = "headStaffUid") String headStaffUid,
            @ApiParam(value = "总部员工名") @RequestParam(value = "headStaffName") String headStaffName,
            @ApiParam(value = "id") @RequestParam(value = "id") Long id
    ) {
//        CourseVideo data = courseVideoService.getById(id);
//        if (data == null) return R.fail("获取失败");
//
//        long l = courseVideoService.dropBean(id);
//        if (l <= 0) return R.failDel();

//        return R.ok();
        return R.fail("此功能已关闭");
    }

    /**
     * 删除阿里云图片
     */
    private void deleteOssFile(String url) {
        if (AccountUtil.judgeProd(envi)) {
            OSSClientUtil ossClientUtil = new OSSClientUtil();
            ossClientUtil.initOss();
            ossClientUtil.deleteFile(OSSClientUtil.privateBucket_subject, url);
        }
    }

}
